// File:       ostream.c++
// Version:    1.01
// Author:     (c) Miles Sabin, 1997
// Purpose:    approximation to ANSI C++ ostream

// Change log:
//  15/02/97   v. 1.00
//  05/03/97   Tidied up buffering in put_double
//  28/03/97   v. 1.01
//             Brought more into line with Dec '96 WP.
//             Functionality bundled into command objects to allow
//               sharing of common exception handling code.

#include "ostream.h"

#include "exception.h"
#include "streambuf.h"
#include "tpltutil.h"

#include "autodstry.c++"


// Implementation of basic_ostream_char

basic_ostream_char::basic_ostream_char(basic_streambuf_char* sb)
  { basic_ios_char::init(sb); }

basic_ostream_char::~basic_ostream_char()
  {}


// Implementation of basic_ostream_char_sentry

INSTANTIATE_CLASS_1(AutoDestroyer, basic_ostream_char_sentry)

basic_ostream_char_sentry::basic_ostream_char_sentry(basic_ostream_char& os)
  : os_(os),
    ok_(false)
  {
    if(os_.good())
    {
      if(os_.tie() != 0)
        os_.tie()->flush();

      if(ios_base::get_sync_with_stdio())
        os_.rdbuf()->sync_stream_with_stdio();

      ok_ = true;
    }
  }

basic_ostream_char_sentry::~basic_ostream_char_sentry()
  {
    if(!uncaught_exception())
    {
      if((os_.flags()&ios::unitbuf) != 0)
        os_.flush();

      if(ios_base::get_sync_with_stdio())
        os_.rdbuf()->sync_stdio_with_stream();
    }
  }

void destroy(basic_ostream_char_sentry* p)
{
  p->~basic_ostream_char_sentry();
}
